//
//  PaySelectViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/25.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

protocol PaySelectViewControllerDelegate {
    func paySelectMethodWithDic(_ dict:NSDictionary)
}

class PaySelectViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var delegate : PaySelectViewControllerDelegate?
    
    var dataArr = NSArray()
    var myTableView = UITableView(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64), style: .plain)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.creatNavView()
        self.getData()
        self.creatTableView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
extension PaySelectViewController{
    fileprivate func creatNavView(){
        self.view.backgroundColor = UIColor.white
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.title = "PAY METHOD"
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "nav_back"), for: UIControlState())
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
        
    }
    
    func leftBtnClick(){
        self.navigationController?.popViewController(animated: true)
    }

}

extension PaySelectViewController{
    fileprivate func getData(){
        
        let parma = ["main_page":"payment_method","zenid":UserSession.shareInstance.cookie!]
        NetworkTool.shareInstance.request(.get, urlString: APIBASE, parmas: parma as [String : NSObject]) { (result, error) in
            if result != nil{
//                let json = JSON(result!)
//                print(json)
                
                let arr = (result as? NSDictionary)!["data"]as?[[String:NSObject]]
                self.dataArr = (arr as? NSArray)!
                
                self.myTableView.reloadData()
            }else{
                SVProgressHUD.showError(withStatus: "get cart data error:" + String(describing: error))
                print("get cart data error:" + String(describing: error))
            }
        }
        
    }
}

extension PaySelectViewController{
    fileprivate func creatTableView(){
        
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            
        }
        cell?.selectionStyle = .none
        
        cell?.textLabel?.text = (dataArr[indexPath.row]as?NSDictionary)!["title"]as?String
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.delegate?.paySelectMethodWithDic((dataArr[indexPath.row]as?NSDictionary)!)
        self.navigationController?.popViewController(animated: true)
    }
}


















